<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html" id="parent">
    <p:commandButton value="Create new item" oncomplete="PF('createNewDiag').show();"/>
    <p:commandButton value="Add row to item" oncomplete="PF('viewer').show();" update="steelItemTitle"/>
    <p:commandButton value="Edit item" oncomplete="PF('editNewDiag').show();"/>
    <p:commandButton value="Edit Row" oncomplete="PF('editChildDiag').show();"/>
    <p:commandButton value="Print" type="button" icon="ui-icon-print" rendered="#{login.current.department.name eq 'Admin'}">
        <p:printer target="steelTable" />
    </p:commandButton>
    <p:dataTable  id="steelTable" var="res" value="#{steelController.steelResources}" 
                  paginator="true" rows="100" 
                  paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                  rowsPerPageTemplate="3,25,50,100,250,500"
                  emptyMessage="No Items Entered"
                  > 
        <f:facet name="header">  
            Steel Doors Data

        </f:facet> 
        <p:columnGroup type="header">  
            <p:row>  
                <p:column rowspan="2" headerText="Item" />  
                <p:column colspan="11" headerText="Item Data" />  
            </p:row>  

            <p:row>  



                <p:column headerText="Quantity" />  
                <p:column headerText="Dimension" />  
                <p:column headerText="Discription" /> 
                <p:column headerText="Rate" /> 
                <p:column headerText="Company" />  
                <p:column headerText="Offer" /> 
                <p:column headerText="Additions" /> 
                <p:column headerText="Total" />  
                <p:column headerText="APrice" /> 
                <p:column headerText="Final" /> 


            </p:row>  
        </p:columnGroup> 
        <p:subTable var="stats" value="#{res.metalDoorsOffersList}" id="subTable1" >  
            <f:facet name="header">
                #{res.title}(#{res.code})

            </f:facet>  
            <p:column>  

            </p:column> 
            <p:column>  
                #{res.quantity}
            </p:column> 
            <p:column>  
                #{res.dimension}
            </p:column> 
            <p:column>  
                #{res.discription}
            </p:column> 
            <p:column>  
                #{res.rate}
            </p:column> 
            <p:column>  
                #{stats.company}  
            </p:column>  
            <p:column>  
                #{stats.offer}  
            </p:column>  

            <p:column>  
                #{stats.additions}
            </p:column>  

            <p:column>  
                #{stats.totalPrice}  
            </p:column>  
            <p:column>  
                #{stats.price}  
            </p:column>
            <p:column>  
                #{stats.finalPrice}  
            </p:column>

        </p:subTable>

    </p:dataTable>

    <p:dialog widgetVar="createNewDiag" header="Create New" dynamic="true" >
        <p:growl  id='steelItemData' life="1500" showDetail="true"/>
        <p:panelGrid columns="2" id="newsteelItem">

            <f:facet name="header">Item Data</f:facet>
            <p:outputLabel for="stellTitle" value="Title"/>
            <p:inputText id="stellTitle" value="#{steelController.title}" required="true" requiredMessage="Title can't be empty"/>
            <p:outputLabel for="stellCode" value="Code"/>
            <p:inputText id="stellCode" value="#{steelController.code}" required="true" requiredMessage="Code can't be empty"/>
            <p:outputLabel for="steelquantity" value="Quantity"/>
            <p:inputText id="steelquantity" value="#{steelController.amount}" required="true" requiredMessage="Qunatity can't be empty"/>
            <p:outputLabel for="steelDimension" value="Dimension"/>
            <p:inputText id="steelDimension" value="#{steelController.dimension}" required="true" requiredMessage="Dimension can't be empty"/>
            <p:outputLabel for="steelARate" value="discription"/>
            <p:inputText id="steelARate" value="#{steelController.discription}" required="true" requiredMessage="Discription can't be empty"/>
            <p:outputLabel for="steelRate" value="Rate"/>
            <p:inputText id="steelRate" value="#{steelController.rate}" required="true" requiredMessage="Rate can't be empty"/>

            <f:facet name="footer">
                <p:commandButton process="newsteelItem" value="Create" update="steelTable,steelItemData,steelItemTitle,editSteelItem" action="#{steelController.save}" oncomplete="handleCreateRequest(xhr, status, args)"/>
            </f:facet>
        </p:panelGrid>
        <script type="text/javascript">
            function handleCreateRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('createNewDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('createNewDiag').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="editNewDiag" header="Edit" dynamic="true" >
        <p:growl  id='editSDItemData' life="3000" showDetail="true"/>
        <p:outputPanel  styleClass="editItemPanel">
            <p:panel id="editSteelItem"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="sditemToEdit" value="Edit Item"/>
                        <p:fragment autoUpdate="true">
                            <h:panelGrid columns="2">
                                <p:selectOneMenu id="sditemToEdit" value="#{steelController.selectedResource}" required="true" converter="steelConverter">
                                    <f:selectItems  var="resItem" value="#{steelController.steelResources}"  itemLabel="#{resItem.title}" itemValue="#{resItem}"/>
                                </p:selectOneMenu> <p:commandButton  process="@(#sditemToEdit)" value="Load Data" update="@(.editItemPanel)" />
                            </h:panelGrid>
                        </p:fragment>
                        <f:facet name="header">Item Data</f:facet>
                        <p:outputLabel for="stellEditTitle" value="Title"/>
                        <p:inputText id="stellEditTitle" value="#{steelController.selectedResource.title}" required="true" requiredMessage="Title can't be empty"/>
                        <p:outputLabel for="stellEditCode" value="Code"/>
                        <p:inputText id="stellEditCode" value="#{steelController.selectedResource.code}" required="true" requiredMessage="Code can't be empty"/>
                        <p:outputLabel for="steelEditquantity" value="Quantity"/>
                        <p:inputText id="steelEditquantity" value="#{steelController.selectedResource.quantity}" required="true" requiredMessage="Qunatity can't be empty"/>
                        <p:outputLabel for="steelEditDimension" value="Dimension"/>
                        <p:inputText id="steelEditDimension" value="#{steelController.selectedResource.dimension}" required="true" requiredMessage="Dimension can't be empty"/>
                        <p:outputLabel for="steelEditARate" value="discription"/>
                        <p:inputText id="steelEditARate" value="#{steelController.selectedResource.discription}" required="true" requiredMessage="Discription can't be empty"/>
                        <p:outputLabel for="steelEditRate" value="Rate"/>
                        <p:inputText id="steelEditRate" value="#{steelController.selectedResource.rate}" required="true" requiredMessage="Rate can't be empty"/>

                        <f:facet name="footer">
                            <p:commandButton process="editItem" value="Edit" update="steelTable,editSDItemData,steelItemTitle,steelItemEditTitle" action="#{steelController.updateItem()}" oncomplete="handleEditDataRequest(xhr, status, args)"/>
                        </f:facet>
                    </p:panelGrid>
                </p:fragment>

            </p:panel>
        </p:outputPanel>
        <script type="text/javascript">
            function handleEditDataRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('editNewDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('editNewDiag').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="viewer" header="Edit" dynamic="true"  style="width: auto;" >
        <p:outputPanel  styleClass="editSteelPanel">
            <p:growl  id='editSteelData' life="1500" showDetail="true"/>

            <p:panel id="steelItemTitle"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="steelItems" value="Offer on Item"/>
                        <p:selectOneMenu id="steelItems" value="#{steelController.selectedResource}" required="true" converter="steelConverter">
                            <f:selectItems  var="resItem" value="#{steelController.steelResources}"  itemLabel="#{resItem.title}" itemValue="#{resItem}"/>
                        </p:selectOneMenu>

                        <p:outputLabel for="scompany" value="Company"/>
                        <p:inputText id="scompany" value="#{steelController.editableItem.company}" required="true" requiredMessage="Company can't be empty"/>
                        <p:outputLabel for="soffer" value="Offer"/>
                        <p:inputText id="soffer" value="#{steelController.editableItem.offer}" required="true" requiredMessage="Offer can't be empty"/>
                        <p:outputLabel for="sadditions" value="Additions"/>
                        <p:inputText id="sadditions" value="#{steelController.editableItem.additions}" required="true" requiredMessage="Additions can't be empty"/>
                        <f:facet name="footer">
                            <p:commandButton process="@(.editSteelData)" value="Create" update="steelTable,@(.editSteelPanel),editSteelData" action="#{steelController.saveData}" oncomplete="handleEditRequest(xhr, status, args);"/>
                        </f:facet>

                    </p:panelGrid>
                </p:fragment>

            </p:panel>


        </p:outputPanel>
        <script type="text/javascript">
            function handleEditRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('viewer').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('viewer').hide();

                }
            }
        </script>
    </p:dialog>
    <p:dialog widgetVar="editChildDiag" header="Edit" dynamic="true"  style="width: auto;" >
        <p:outputPanel  styleClass="editChildPanel">
            <p:growl  id='editStChildData' life="1500" showDetail="true"/>

            <p:panel id="steelItemEditTitle"  style="overflow: auto">
                <p:fragment autoUpdate="true">
                    <p:panelGrid columns="2"  >
                        <p:outputLabel for="metalchilditems" value="Row to Edit"/>
                        <p:fragment autoUpdate="true">
                            <h:panelGrid columns="2">

                                <p:selectOneMenu id="metalchilditems" value="#{steelController.editableItem}" required="true" converter="metalOffersConverter">
                                    <f:selectItems  var="resItem" value="#{steelController.getChildResources()}"  itemLabel="#{resItem.company}" itemValue="#{resItem}"/>
                                </p:selectOneMenu>

                                <p:commandButton  process="@(#metalchilditems)" value="Load Data" update="@(.editChildPanel)" />
                            </h:panelGrid>
                        </p:fragment>

                        <p:outputLabel for="sEditcompany" value="Company"/>
                        <p:inputText id="sEditcompany" value="#{steelController.editableItem.company}" required="true" requiredMessage="Company can't be empty"/>
                        <p:outputLabel for="sEditoffer" value="Offer"/>
                        <p:inputText id="sEditoffer" value="#{steelController.editableItem.offer}" required="true" requiredMessage="Offer can't be empty"/>
                        <p:outputLabel for="sEditadditions" value="Additions"/>
                        <p:inputText id="sEditadditions" value="#{steelController.editableItem.additions}" required="true" requiredMessage="Additions can't be empty"/>
                        <f:facet name="footer">
                            <p:commandButton process="@(.editChildPanel)" value="Create" update="steelTable,@(.editChildPanel),editChildData" action="#{steelController.updateChildItem()}" oncomplete="handleEditRequest(xhr, status, args);"/>
                        </f:facet>

                    </p:panelGrid>
                </p:fragment>

            </p:panel>


        </p:outputPanel>
        <script type="text/javascript">             function handleEditRequest(xhr, status, args) {
                if (args.validationFailed) {
                    PF('editChildDiag').jq.effect("shake", {times: 5}, 100);
                }
                else {
                    PF('editChildDiag').hide();

                }
            }
        </script>
    </p:dialog>
</ui:composition>

